<template>
  <a-layout-header class="header">
    <div class="header-content">
      <div class="logo">
        <router-link to="/" class="logo-link">
          <a-typography-title :level="3" class="site-title">
            战神校园二手交易平台
          </a-typography-title>
        </router-link>
      </div>
      <a-input-search
          v-model:value="searchValue"
          placeholder="搜索战神平台"
          enter-button
          @search="onSearch"
          class="search"
      >
        <template #enterButton>
          <SearchOutlined />
        </template>
      </a-input-search>
      <div class="nav-links">
        <a-space size="large">
          <router-link to="/UserCenter" class="cart-link">
            <a-badge :count="cartItemCount">
              <UserOutlined class="icon" />
            </a-badge>
            个人中心
          </router-link>
          <a-dropdown v-if="userInfo.userId">
            <a class="ant-dropdown-link user-dropdown" @click.prevent>
              <a-avatar :src="userInfo.avatar" :alt="userInfo.username" />
              <span class="username">{{ userInfo.username }}</span>
              <DownOutlined />
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="0">
                  <router-link to="/UserCenter">个人中心</router-link>
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="1" @click="logout">注销</a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
          <a-button v-else type="primary" @click="login" size="large">
            登录
          </a-button>
        </a-space>
      </div>
    </div>
  </a-layout-header>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useUserStore } from '@/stores/index.js';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { UserOutlined, SearchOutlined, DownOutlined } from '@ant-design/icons-vue';

const userStore = useUserStore();
const router = useRouter();
const searchValue = ref('');

const userInfo = computed(() => userStore.userInfo);

const cartItemCount = ref(1); // 这里应该从store中获取实际的购物车数量

const onSearch = (value) => {
  console.log('Searching for:', value);
  // Implement search functionality here
};

const login = () => {
  router.push('/login');
};

const logout = () => {
  userStore.logout();
  message.success('注销成功');
  router.push('/');
};
</script>

<style scoped>
.header {
  padding: 0;
  background: linear-gradient(135deg, #FF6600, #FF9966);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 1000px;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80px;
  padding: 0 24px;
}

.logo-link {
  text-decoration: none;
}

.site-title {
  margin: 0;
  color: #FFFFFF !important;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.search {
  width: 400px;
}

.search :deep(.ant-input) {
  border-radius: 20px 0 0 20px;
  height: 40px;
}

.search :deep(.ant-input-search-button) {
  border-radius: 0 20px 20px 0;
  height: 40px;
  width: 60px;
}

.nav-links {
  display: flex;
  align-items: center;
}

.cart-link {
  display: flex;
  align-items: center;
  color: #FFFFFF;
  font-size: 16px;
  text-decoration: none;
  transition: all 0.3s ease;
}

.cart-link:hover {
  opacity: 0.8;
}

.cart-link .icon {
  font-size: 24px;
  margin-right: 8px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  color: #FFFFFF;
  font-size: 16px;
  cursor: pointer;
}

.user-dropdown .username {
  margin: 0 8px;
}

.user-dropdown :deep(.anticon) {
  font-size: 12px;
}

@media (max-width: 1024px) {
  .header-content {
    flex-wrap: wrap;
    height: auto;
    padding: 16px 24px;
  }

  .logo, .search, .nav-links {
    margin-bottom: 16px;
  }

  .search {
    width: 100%;
    order: 3;
  }

  .nav-links {
    order: 2;
  }
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    align-items: stretch;
  }

  .nav-links {
    justify-content: space-between;
  }
}
</style>